package com.bsoft.gol.hcs.clinical.dao;


import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import pcn.prescribe.OdsPrescriptionInfo;

import java.util.List;


public abstract class OdsPrescriptionInfoDAO extends HibernateSupportDelegateDAO<OdsPrescriptionInfo> {
    @DAOMethod(sql = "from OdsPrescriptionInfo where consultId =:consultId and auditStatus=:auditStatus and deleteFlag='0'")
    public abstract List<OdsPrescriptionInfo> queryPrescriptionByAuditStatus(@DAOParam("consultId") String consultId, @DAOParam("auditStatus") String auditStatus);

    @DAOMethod(sql = "select count(*) from OdsPrescriptionInfo where localDoctorId=:doctorId and auditStatus='0'")
    public abstract long getCountByAuditDoctorId(@DAOParam("doctorId") String doctorId);

    @DAOMethod(sql = " from OdsPrescriptionInfo where prescriptionInfoId =:prescriptionInfoId ")
    public abstract OdsPrescriptionInfo getOdsPrescriptionInfo(@DAOParam("prescriptionInfoId") String prescriptionInfoId);

    @DAOMethod(sql = "select count(1) from OdsPrescriptionInfo where consultId=:consultId and deleteFlag='0'")
    public abstract long getCountByConsultId(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = "from OdsPrescriptionInfo where consultId =:consultId and auditStatus=:auditStatus and prescriptionStatus='1' and  deleteFlag='0'")
    public abstract List<OdsPrescriptionInfo> queryByAuditStatusAndConsultId(@DAOParam("consultId") String consultId, @DAOParam("auditStatus") String auditStatus);


    @DAOMethod(sql = " from OdsPrescriptionInfo where orgId =:orgId and  prescriptionNumber =:prescriptionNumber  ")
    public abstract OdsPrescriptionInfo getOdsPrescriptionInfoByPrescriptionNumber(@DAOParam("orgId") String orgId, @DAOParam("prescriptionNumber") String prescriptionNumber);

    @DAOMethod(sql = " from OdsPrescriptionInfo where orgId =:orgId and  prescriptionNumber in:prescriptionNumberList  ")
    public abstract List<OdsPrescriptionInfo> queryOdsPrescriptionInfoByPrescriptionNumberList(@DAOParam("orgId") String orgId, @DAOParam("prescriptionNumberList") List<String> prescriptionNumberList);


    @DAOMethod(sql = " from OdsPrescriptionInfo where orgId =:orgId and  prescriptionId =:prescriptionId  ")
    public abstract OdsPrescriptionInfo getOdsPrescriptionInfoByPrescriptionId(@DAOParam("orgId") String orgId, @DAOParam("prescriptionId") String prescriptionId);

    @DAOMethod(sql = "update OdsPrescriptionInfo set prescriptionId=:prescriptionId,prescriptionNumber=:prescriptionNumber where prescriptionInfoId =:prescriptionInfoId")
    public abstract void updatePrescriptionStatusById(@DAOParam("prescriptionId")String prescriptionId,@DAOParam("prescriptionNumber")String prescriptionNumber ,@DAOParam("prescriptionInfoId")String prescriptionInfoId);

    @DAOMethod(sql = "update OdsPrescriptionInfo set prescriptionStatus='0',auditStatus='0'  where prescriptionInfoId in:prescriptionInfoIdList")
    public abstract void updatePrescriptionStatusForFailed(@DAOParam("prescriptionInfoIdList") List<String> prescriptionInfoIdList);

    @DAOMethod(sql = " from OdsPrescriptionInfo where prescriptionId =:prescriptionId and  prescriptionNumber =:prescriptionNumber and orgId=:orgId and  epitaxialOrganizationId =:epitaxialOrganizationId  ")
    public abstract OdsPrescriptionInfo getOdsPrescriptionInfoByPrescriptionNumberAndId(@DAOParam("orgId") String orgId, @DAOParam("prescriptionNumber") String prescriptionNumber
            , @DAOParam("prescriptionId") String prescriptionId, @DAOParam("epitaxialOrganizationId") String epitaxialOrganizationId);


    public  List<OdsPrescriptionInfo> queryOdsPrescriptionInfo(final OdsPrescriptionInfo info){
        HibernateStatelessResultAction<List<OdsPrescriptionInfo>> action = new AbstractHibernateStatelessResultAction<List<OdsPrescriptionInfo>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("select *  from ods_prescription_info where 1=1 ");
                if (StringUtils.isNotEmpty(info.getAuditStatus())) {
                    stringBuffer.append(" and audit_status = '" + info.getAuditStatus()+"'");
                }
                if(StringUtils.isNotEmpty(info.getPrescriptionInfoId())) {
                    stringBuffer.append(" and prescription_info_id = '" + info.getPrescriptionInfoId() + "'");
                }else{
                    stringBuffer.append(" and consult_id = '" + info.getConsultId() + "'");
                    stringBuffer.append(" and consult_type = '" + info.getConsultType() + "'");
                }
                stringBuffer.append(" and delete_flag = '0' and prescription_status='1' ");
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                sqlQuery.addEntity(OdsPrescriptionInfo.class);
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
